package com.ruoyi.intelligence.chat.aliyun;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.MessageManager;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: wxj
 * 通义千问
 * @Date: 2023/11/23
 */
@Component
public class TyqwFactory {
    public  void callWithMessage() throws NoApiKeyException, ApiException, InputRequiredException {
        Generation gen = new Generation();
        MessageManager msgManager = new MessageManager(10);
        Message systemMsg =
            Message.builder().role(Role.SYSTEM.getValue()).content("You are a helpful assistant.").build();
        Message userMsg = Message.builder().role(Role.USER.getValue()).content("你好，周末去哪里玩？").build();
        msgManager.add(systemMsg);
        msgManager.add(userMsg);
        QwenParam param =
            QwenParam.builder().model(Generation.Models.QWEN_PLUS).messages(msgManager.get())
                .resultFormat(QwenParam.ResultFormat.MESSAGE)
                .topP(0.8)
                .apiKey("sk-010ca67d2a35422cba1f0fe9d214dd96")
                .enableSearch(true)
                .build();

        GenerationResult result = gen.call(param);
        System.out.println(result);
        msgManager.add(result);
        System.out.println(JsonUtils.toJson(result));
        param.setPrompt("找个近点的");
        param.setMessages(msgManager.get());
        result = gen.call(param);
        System.out.println(result);
        System.out.println(JsonUtils.toJson(result));
    }
    public  String callWithListMessage(List<String> msgList,String newMsg) throws NoApiKeyException, ApiException, InputRequiredException {

        Generation gen = new Generation();
        MessageManager msgManager = new MessageManager(10);
        Message systemMsg = Message.builder().role(Role.SYSTEM.getValue()).content(msgList.get(0)).build();
        msgManager.add(systemMsg);
        if (msgList.size()>0){
            for (int i = 0; i < msgList.size(); i++) {
                Message userMsg = Message.builder().role(Role.USER.getValue()).content(msgList.get(i)).build();
                msgManager.add(userMsg);
            }
        }
        QwenParam param =
            QwenParam.builder().model(Generation.Models.QWEN_PLUS).messages(msgManager.get())
                .resultFormat(QwenParam.ResultFormat.MESSAGE)
                .topP(0.8)
                .apiKey("sk-010ca67d2a35422cba1f0fe9d214dd96")
                .enableSearch(true)
                .build();
        GenerationResult result = gen.call(param);
        msgManager.add(result);
        if (ObjectUtil.isNotEmpty(newMsg)){
            param.setPrompt(newMsg);
            param.setMessages(msgManager.get());
            result = gen.call(param);
        }
        return result.getOutput().getChoices().get(0).getMessage().getContent();
    }




//    public static void main(String[] args){
//        try {
//            callWithMessage();
//        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
//            System.out.println(e.getMessage());
//        }
//        System.exit(0);
//    }
}
